package com.mrjy.common.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateUtil {
	/**
	 * yyyy-MM-dd
	 */
	public final static int YEAR_MONTH_DAY = 1;
	/**
	 * yyyy-MM-dd HH:mm:ss
	 */
	public final static int FULL_DATE = 2;	
	/**
	 * HH:mm:ss
	 */
	public final static int HOUR_MINUTE_SECOND = 3;
	/**
	 * yyyyMMddHHmmss
	 */
	public final static int FULL_DATE_STR = 4;
	/**
	 * yyyy-MM-dd HH:mm
	 */
	public final static int FULL_DATE_NO_SECOND = 5;

    /**
     * Date转String
     *
     * @param date
     * @return
     */
    public static String dateFormat(Date date) {
        String result="";
        long now = new Date().getTime();
        long qt = 0;//时间差
        qt = (now - date.getTime())/1000;
        if (qt < 60) {
            result ="刚刚";
        }else if ((qt /= 60) < 60 ) {
            result=qt +"分钟前";
        }else if ((qt /= 60) < 24) {
            result=qt +"小时前";
        }else if ((qt /= 24) < 5) {
            result=qt +"天前";
        }else {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
            result = sdf.format(date);
        }
        return result;
    }


	/**
	 * Date转String
	 * 
	 * @param date
	 * @param type
	 * @return
	 */
	public static String dateToString(Date date, int type) {
		SimpleDateFormat df;
		if (type == YEAR_MONTH_DAY) {
			df = new SimpleDateFormat("yyyy-MM-dd");
			return df.format(date);
		} else if (type == FULL_DATE) {
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			return df.format(date);
		}else if (type == FULL_DATE_NO_SECOND) {
			df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			return df.format(date);
		} else if (type == HOUR_MINUTE_SECOND) {
			df = new SimpleDateFormat("HH:mm:ss");
			return df.format(date);
		} else if (type == FULL_DATE_STR) {
			df = new SimpleDateFormat("yyyyMMddHHmmss");
			return df.format(date);
		}else {
			return date.toString();
		}
	}
		
	/**
	 * long转String
	 * 
	 * @param long
	 * @param type
	 * @return
	 */
	public static String longToString(long l, int type) {
		return dateToString(new Date(l),type);
	}
	
	/**
	 * String转Date
	 * 字符串格式："0000/00/00 00:00:00"
	 * @param String
	 * @return
	 */
	public static Date stringToDate(String str,int type) {
		SimpleDateFormat sdf = null;
		Date date=null;
		try{					
		if(type==YEAR_MONTH_DAY){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd" );
			  date = sdf.parse(str);			
		}else if(type==FULL_DATE){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  date = sdf.parse(str);
		}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	
	/**
	 * String转long
	 * 字符串格式："yyyy-MM-dd HH:mm:ss" 或  "yyyy-MM-dd"
	 * @param str 时间字符串
	 * @param type 时间类型
	 * @return
	 */
	public static long stringToLong(String str,int type) {
		SimpleDateFormat sdf = null;
		Date date=null;
		try{					
		if(type==YEAR_MONTH_DAY){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd" );
			  date = sdf.parse(str);			
		}else if(type==FULL_DATE){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			  date = sdf.parse(str);
		}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date.getTime();
	}
	
	
	/**
	 * 简单验证日期格式
	 * @param date
	 * @return
	 */
	public static boolean dateFormate(String date){
		String eL = "[0-9]{4}-[0-9]{2}-[0-9]{2}";
		Pattern p = Pattern.compile(eL);
		Matcher m = p.matcher(date);
		boolean dateFlag = m.matches();
		return dateFlag;
	}
	/**
	 * 根据时间戳 判断是否为本日、本月或本年
	 * @param time
	 * @param pattern yyyy-MM-dd（本日），yyyy-MM(本月) , yyyy(本年)
	 * @return
	 */
	public static boolean isThisTime(long time,String pattern) {  
        Date date = new Date(time);  
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);  
        String param = sdf.format(date);//参数时间  
        String now = sdf.format(new Date());//当前时间  
        if(param.equals(now)){  
           return true;  
        }  
        return false;  
    }  
	/**
	 * 当前时间加上年份后返回毫秒数
	 * @param time 毫秒数
	 * @param years 年份
	 * @return
	 */
	public static Long dateTimeAddYears(long time,int years){
		Calendar calendar = Calendar.getInstance();
        Date date = new Date(time);
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        date = calendar.getTime();
        return date.getTime();
	}
	
	/**
	 * 当前时间加上年份后返回date
	 * @param date 时间对象
	 * @param years 年份
	 * @return
	 */
	public static Date dateAddYears(Date date,int years){
		Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, years);
        date = calendar.getTime();
        return date;
	}
	
    /**
     * 根据时间戳判断两时间是否是同一天
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(Long time1,Long time2){
    	boolean flag = false ; 
    	if(null == time1 || null == time2)  
            return flag; 
    	
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Date d1 = new Date(time1);
    	Date d2 = new Date(time2);
    	flag = sdf.format(d1).equals(sdf.format(d2));
        return flag;
    }
    /**
     * 判断两个时间是否前后一天（time1天数加一天和time2比较）
     * @param time1 前一天的时间
     * @param time2 后一天的时间
     * @return
     */
    public static boolean isBeforDay(Long time1,Long time2){
    	boolean flag = false ; 
    	if(null == time1 || null == time2)  
            return flag; 
    	Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(time1);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
      
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	Date d1 = calendar.getTime();
    	Date d2 = new Date(time2);
    	flag = sdf.format(d1).equals(sdf.format(d2));
        return flag;
    }
    /**
	 * 当前时间加上月份后返回毫秒数
	 * @param time 毫秒数
	 * @param months 月数
	 * @return
	 */
	public static Long dateTimeAddMonths(long time,int months){
		Calendar calendar = Calendar.getInstance();
        Date date = new Date(time);
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, months);
        date = calendar.getTime();
        return date.getTime();
	}
	/**
	 * 当前时间加上天数后返回毫秒数
	 * @param time 毫秒数
	 * @param days 天数 
	 * @return
	 */
	public static Long dateTimeAddDays(long time,int days){
		Calendar calendar = Calendar.getInstance();
        Date date = new Date(time);
        calendar.setTime(date);
        calendar.add(Calendar.DATE, days);
        date = calendar.getTime();
        return date.getTime();
	}
    
    /*** 
     * 日期减一天、加一天 
     *  
     * @param option 
     *            传入类型 pre：日期减一天，next：日期加一天 
     * @param _date 
     *            2014-11-24 
     * @param type 
     * @return 减一天：2014-11-23或(加一天：2014-11-25) 
     */ 
    public static String checkOption(String option, String _date, int num,int type) {  
        SimpleDateFormat sdf = null;
        if(type==YEAR_MONTH_DAY){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd" );
		}else if(type==FULL_DATE){
			  sdf =   new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		}
        Calendar cl = Calendar.getInstance();  
        Date date = null;  
  
        try {  
            date = (Date) sdf.parse(_date);  
        } catch (ParseException e) {  
            e.printStackTrace();  
        }  
        cl.setTime(date);  
        if ("pre".equals(option)) {  
            // 时间减一天  
            cl.add(Calendar.DAY_OF_MONTH, -num);  
  
        } else if ("next".equals(option)) {  
            // 时间加一天  
            cl.add(Calendar.DAY_OF_YEAR, num);  
        } else {  
            // do nothing  
        }  
        date = cl.getTime();  
        return sdf.format(date);  
    } 
    
    /**
     * 判断时间是否在时间段内
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public static boolean betweenTime() {
    	SimpleDateFormat df = new SimpleDateFormat("HH:mm");//设置日期格式
        Date nowTime =null;
        Date beginTime = null;
        Date endTime = null;
        try {
        	nowTime = df.parse(df.format(new Date()));
            beginTime = df.parse("06:00");
            endTime = df.parse("23:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }
    
    /**
     * 比较两个时间(字符串)的大小 格式:yyyy-MM-dd HH:mm:ss  
     * @param dateStr1
     * @param dateStr2
     * @return "surpass":dateStr1大于dateStr2  "equal":等于   "less":小于
     *
     */
    public static String compareTime(String dateStr1, String dateStr2){
    	try {
    		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date date1 = sdf.parse(dateStr1);
			Date date2 = sdf.parse(dateStr2);
			if (date1.getTime()-date2.getTime()>0) {
				return "surpass";
			}else if (date1.getTime()-date2.getTime()==0) {
				return "equal";
			}else {
				return "less";
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
    }
}
